﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using BCNPCObject.DB;
using DbLinq;
using DbLinq.Oracle;
using CMISLibrary.Entities;
using CMISLibrary;

namespace BCNPCObject
{
    public class cls_D_DVI_QLY_Controller
    {
        #region   Atributes

        private DataSet CMIS_Header = new CMISOutputParameter();
        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();
        private D_DVI_QLY info;

        #endregion

        #region   Constructor

        public cls_D_DVI_QLY_Controller()
        {
            info = new D_DVI_QLY();
        }
        public cls_D_DVI_QLY_Controller(D_DVI_QLY Info)
        {
            this.info = Info;
        }

        #endregion

        #region   Properties

        public D_DVI_QLY pInfor
        {
            get { return info; }
            set { info = value; }
        }

        #endregion
     
        #region  DungNT NPC
        public DataSet select_D_DVI_QLY_BYMADVI(string strMadviqly)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            try
            {
                var q1 = (from p in CMIS2.DB.D_DVI_QLY
                          where p.MA_DVIQLY.Contains(strMadviqly)
                          select new
                          {
                              p.MA_DVIQLY,
                              p.TEN_DVIQLY,
                              p.MA_DVICTREN,

                          }).ToList();
                var q2 = from p in q1
                         orderby p.MA_DVIQLY
                         select new
                         {
                             p.MA_DVIQLY,
                             p.TEN_DVIQLY,
                             p.MA_DVICTREN,
                             TEN_DVIQLYCTREN = p.MA_DVICTREN == null ? "" :
                                               CMIS2.DB.D_DVI_QLY.Where(m => m.MA_DVIQLY == p.MA_DVICTREN).Select(m => m.TEN_DVIQLY).AsEnumerable().FirstOrDefault()
                         };
                dt = Utility.LINQToDataTable(q2);
                ds.Tables.Add(dt);

                //var q2 = from p in CMIS2.DB.D_DVI_QLY where p.MA_DVIQLY == strMadviqly

                return ds;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }
        public DataSet Get_DM_DVIQLY(string strMadviql)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            try
            {
                var q = from p in CMIS2.DB.D_DVI_QLY
                        where p.MA_DVIQLY.Contains(strMadviql)
                        select new
                        {
                            p.MA_DVIQLY,
                            p.TEN_DVIQLY
                        };
                dt = Utility.LINQToDataTable(q);
                ds.Tables.Add(dt);
                return ds;
            }
            catch
            {
                CMIS2.ResetDB();
                return null;
            }
        }
        public DataSet select_D_DVI_QLY_ALL(String strMa_dviqly)
        {
            try
            {

                var q = from p in CMIS2.DB.D_DVI_QLY
                        orderby p.MA_DVIQLY ascending
                        where (p.MA_DVIQLY.Contains(strMa_dviqly))
                        select new 
                        { 
                            p.MA_DVIQLY,
                            p.TEN_DVIQLY,
                            p.MA_DVICTREN
                        };

                DataTable dt = Utility.LINQToDataTable(q);
                DataSet ds = new DataSet();
                dt.TableName = "D_DVI_QLY";
                dt.AcceptChanges();
                ds.Tables.Add(dt);
                return ds;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }
        #endregion
    }
}
